/*
 * LayerGroup.java
 *
 * Created on January 9, 2008, 7:47 PM
 */

package picto.core;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import picto.io.Archivable;
import picto.io.ArchiveException;
import picto.io.ArchiveReader;
import picto.io.ArchiveWriter;

/**
 *
 * @author davedes
 */
public class LayerGroup implements Archivable, Iterable<Layer> {
    
    private List<Layer> layers;
    private List<Layer> unmod;
    
    /**
     * Creates a new instance of LayerGroup
     */
    public LayerGroup(int num) {
        layers = new ArrayList<Layer>(num);
        unmod = Collections.unmodifiableList(layers);
    }
    
    /** Creates a new layer list with an initial capacity of 5 layers. */
    public LayerGroup() {
        this(5);
    }
    
    public Layer get(int index) {
        return layers.get(index);
    }
    
    public boolean add(Layer layer) {
        return add(size(), layer);
    }
    
    public boolean add(int index, Layer layer) {
        if (layer!=null) {
            layers.add(index, layer);
            return true;
        } else
            return false;
    }
    
    public boolean remove(Layer layer) {
        if (layer!=null)
            return layers.remove(layer);
        else
            return false;
    }
    
    public void remove(int index) {
        remove(layers.get(index));
    }
    
    public int indexOf(Layer layer) {
        return layers.indexOf(layer);
    }
    
    public int size() {
        return layers.size();
    }
    
    public void removeAll() {
        for (int i=size()-1; i>=0; i--)
            remove(i);
    }
    
    public Iterator<Layer> iterator() {
        return unmod.iterator();
    }
    
    public List<Layer> layers() {
        return unmod;
    }
    
    public void dispose() {
        for (int i=0; i<layers.size(); i++) {
            ArchivableImage img = layers.get(i).getImage();
            if (img!=null)
                img.dispose();
        }
    }

    public Element write(ArchiveWriter writer) throws ArchiveException {
        Document doc = writer.getDocument();
        Element root = doc.createElement("layer-group");
        
        for (int i=0; i<size(); i++) {
            Layer layer = get(i);
            if (layer!=null) {
                Element child = layer.write(writer);
                root.appendChild(child);
            }
        }
        
        return root;
    }

    public void read(ArchiveReader reader, Element root) throws ArchiveException {
        if (!"layer-group".equals(root.getNodeName()))
            throw new IllegalArgumentException("LayerGroup must be parsed from <layer-group> tag");
        
        removeAll();
        
        NodeList nl = root.getChildNodes();
        for (int i=0; i<nl.getLength(); i++) {
            Node n = nl.item(i);
            if (n instanceof Element && "layer".equals(n.getNodeName())) {
                Element e = (Element)n;
                Layer layer = new Layer();
                layer.read(reader, (Element)n);
                this.add(layer);
            }
        }
    }
    
}
